﻿using System;
using static System.Console;

namespace Wrox.ProCSharp.Delegates
{
    //定义DoubleOp委托类
    delegate double DoubleOp(double x);

    class Program
    {
        static void Main()
        {
            //实例化DoubleOp委托的数组
            //DoubleOp[] operations =
            //{
            //    MathOperations.MultiplyByTwo,
            //    MathOperations.Square
            //};
            
            //使用Func<T>委托
            Func<double,double>[] operations =
            {
                MathOperations.MultiplyByTwo,
                MathOperations.Square
            };
            

            for (int i = 0; i < operations.Length; i++)
            {
                WriteLine($"Using operations[{i}]:");
                //WriteLine($"{operations[i](2.0)}");
                ProcessAndDisplayNumber(operations[i], 2.0);
                //WriteLine($"{operations[i](7.94)}");
                ProcessAndDisplayNumber(operations[i], 7.94);
                //WriteLine($"{operations[i](1.414)}");
                ProcessAndDisplayNumber(operations[i], 1.414);
                WriteLine();
                
            }
        }
        //static void ProcessAndDisplayNumber(DoubleOp action, double value)
        //{
        //    double result = action(value);
        //    WriteLine($"Value is {value}, result of operation is {result}");
        //}
        static void ProcessAndDisplayNumber(Func<double, double> action, double value)
        {
            double result = action(value);
            WriteLine($"Value is {value}, result of operation is {result}");
        }
    }
}
